from typing import Dict, List
from datetime import datetime


class TradeRecorder:
    """交易记录模块"""

    def __init__(self):
        self.trades: List[Dict] = []

    def record_trade(
        self,
        symbol: str,
        name: str,
        order_price: float,
        trade_price: float,
        volume: int,
        direction: str,
        amount: float,
        commission: float = 0.0
    ) -> None:
        """记录交易信息"""
        trade = {
            "symbol": symbol,
            "name": name,
            "order_price": order_price,
            "trade_price": trade_price,
            "volume": volume,
            "direction": direction,
            "amount": amount,
            "commission": commission,
            "time": datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        }
        self.trades.append(trade)

    def get_trades(self) -> List[Dict]:
        """获取所有交易记录"""
        return self.trades.copy()

    def clear_trades(self) -> None:
        """清空交易记录"""
        self.trades.clear()

    def save_data(self) -> Dict:
        """返回交易记录数据"""
        return self.trades.copy()
        
    def load_data(self, data: Dict) -> None:
        """加载交易记录数据"""    
        self.trades = data.copy()